package edu.scau.mis.web.controller.pos;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import edu.scau.mis.core.annotation.RequestLog;
import edu.scau.mis.core.domain.AjaxResult;
import edu.scau.mis.core.enums.BusinessType;
import edu.scau.mis.pos.domain.Category;

import edu.scau.mis.pos.service.ICategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("category")
@Tag(name = "CategoryController ", description = "类别")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;

    @RequestLog(title = "查询所有类别",businessType = BusinessType.Query)
    @GetMapping("/tree")
    @Operation(summary = "getCategoryTree" ,
            description = "查询所有类别，返回树形数据")
    public AjaxResult getCategoryTree() {
        return AjaxResult.success(categoryService.getCategoryTree());
    }

    @RequestLog(title = "根据ID查询类别",businessType = BusinessType.Query)
    @GetMapping("/getById/{categoryId}")
    @Operation(summary = "根据ID查询类别" ,
            description = "根据ID查询类别，返回树形数据",
            parameters = {
                    @Parameter(name = "categoryId", description = "类别编号")
            })
    public AjaxResult getCategoryById(@PathVariable Long categoryId) {
        return AjaxResult.success(categoryService.getCategoryById(categoryId));
    }
    @RequestLog(title = "根据名称查询类别",businessType = BusinessType.Query)
    @GetMapping("/getByName/{categoryName}")
    @Operation(summary = "根据类别名称查询类别" ,
            description = "根据类别名称查询类别，返回树形数据",
            parameters = {
                    @Parameter(name = "categoryName", description = "类别名称")
            })
    public AjaxResult getCategoryById(@PathVariable String categoryName) {
        return AjaxResult.success(categoryService.getCategoryByName(categoryName));
    }

    @RequestLog(title = "查询一级类别",businessType = BusinessType.Query)
    @GetMapping("/getParent")
    @Operation(summary = "查询所有大类",
            description = "查询所有大类，如男装、上衣，不包括最小类")
    public AjaxResult getTopLevelCategory() {
        return AjaxResult.success(categoryService.getTopLevelCategory());
    }

    @RequestLog(title = "根据ID删除类别",businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoryId}")
    @Operation(summary = "根据ID删除类别",description = "根据ID删除类别",
            parameters = {
                    @Parameter(name = "categoryId", description = "类别编号")
            })
    public AjaxResult remove(@PathVariable Long categoryId) {
        int rows = categoryService.deleteCategoryById(categoryId);
        return rows > 0 ? AjaxResult.success("删除产品类别成功") : AjaxResult.error("删除产品类别失败");
    }

    @RequestLog(title = "批量删除类别",businessType = BusinessType.DELETE)
    @DeleteMapping("/delBatch/{categoryIds}")
    @Operation(summary = "批量删除类别",description = "根据ID集合删除类别",
            parameters = {
                    @Parameter(name = "categoryIds", description = "类别编号集合")
            })
    public AjaxResult remove(@PathVariable Long[] categoryIds) {
        int rows = categoryService.deleteCategoryByIds(categoryIds);
        return rows > 0 ? AjaxResult.success("批量删除产品类别成功") : AjaxResult.error("批量删除产品类别失败");
    }

    @RequestLog(title = "插入类别",businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    @Operation(summary = "插入类别")
    public AjaxResult add(@RequestBody Category category) {
        int rows = categoryService.insertCategory(category);
        return rows > 0 ? AjaxResult.success("添加产品类别成功") : AjaxResult.error("添加产品类别失败");
    }
    @RequestLog(title = "修改类别",businessType = BusinessType.UPDATE)
    @PutMapping
    @Operation(summary = "修改类别")
    public AjaxResult edit(@RequestBody Category category) {
        int rows = categoryService.updateCategory(category);
        return rows > 0 ? AjaxResult.success("修改产品类别成功") : AjaxResult.error("修改产品类别失败");
    }
    @RequestLog(title = "分页查询类别",businessType = BusinessType.Query)
    @GetMapping("/page")
    @Operation(summary = "对类别分页", description = "前端插入pageNum,pageSize,categoryName,categoryId")
    public AjaxResult page(
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam(value = "categoryName", required = false) String categoryName,
            @RequestParam(value = "categoryId", required = false) Long categoryId
    ) {
        // 分页查询顶层节点
        PageHelper.startPage(pageNum, pageSize);
        Category queryParam = new Category();
        List<Category> topLevelNodes = categoryService.getTopCategory();
        PageInfo<Category> pageInfo = new PageInfo<>(topLevelNodes);

        // 查询每个顶层节点的子节点
        for (Category topNode : topLevelNodes) {
            topNode.setChildren(categoryService.findChildren(topNode.getCategoryId()));
        }
        System.out.println(pageInfo);
        return AjaxResult.success(pageInfo);
    }
}
